﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
namespace WebApplication1
{
    public partial class AddCommitteeMembers : System.Web.UI.Page
    {


        AdminAL adminAL = new AdminAL();
        MemberAL memberAL = new MemberAL();
        public Guid SelectedMember 
        { 
            get
            {
                if (Session["MemberId"] == null)
                {
                    return memberAL.GetAllMembers()[0].MemberId;
                }
                else
                {
                    return new Guid(Session["MemberId"].ToString());
                }
            }
            set
            {
                Session["MemberId"] = value;
            }
        }

        public Guid SelectedCommittee
        {
            get
            {
                if (Session["CommitteeId"] == null)
                {
                    return memberAL.GetAllCommittees()[0].CommitteeId;
                }
                else
                {
                    return new Guid(Session["CommitteeId"].ToString());
                }
            }
            set
            {
                Session["CommitteeId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
                base.OnPreRenderComplete(e);

                List<CommitteeDTO> committeeDTO = memberAL.GetAllCommittees().OrderByDescending(a=> a.StartDate).ToList();
                List<MemberDTO> memberDTO = memberAL.GetAllMembers().OrderBy(b=> b.FirstName).ToList();
                ddlCommMembrCommId.DataSource = committeeDTO;
                ddlCommMembrCommId.DataTextField = "Title";
                ddlCommMembrCommId.DataValueField = "CommitteeId";
                ddlCommMembrCommId.SelectedValue = SelectedCommittee.ToString();
                ddlCommMembrCommId.DataBind();


                ddlCommMembrMembrId.DataSource = memberDTO;
                ddlCommMembrMembrId.DataTextField = "FullName";
                ddlCommMembrMembrId.DataValueField = "MemberId";
                ddlCommMembrMembrId.SelectedValue = SelectedMember.ToString();
                ddlCommMembrMembrId.DataBind();
            //ddlCommMembrCommId.Items.Add(new ListItem("Select", "0"));
            //if (ddlCommMembrCommId.Items.FindByValue("0").Selected != null)
            //{
            //    ddlCommMembrCommId.ClearSelection();
            //    ddlCommMembrCommId.Items.FindByValue("0").Selected = true;
            //}
        }

        protected void Page_Load(object sender, EventArgs e)
        {

   
        }

       
        protected void ddlCommMembrMembrId_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedMember = new Guid(ddlCommMembrMembrId.SelectedValue);
        }

        protected void ddlCommMembrCommId_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedCommittee = new Guid(ddlCommMembrCommId.SelectedValue);
        }

        protected void btnAddComMembrSubmit_Click(object sender, EventArgs e)
        {
            MemberAL memberAL = new MemberAL();
            DateTime membrStartDate = DateTime.MinValue;
            DateTime membrEndDate = DateTime.MinValue;
            DateTime commStart = memberAL.GetCommitteeById(new Guid(ddlCommMembrCommId.SelectedValue)).SingleOrDefault().StartDate;
            DateTime commEnd = memberAL.GetCommitteeById(new Guid(ddlCommMembrCommId.SelectedValue)).SingleOrDefault().EndDate;
            try
            {
                if (txtCommMembrStartDate.Text == "" || txtCommMembrEnDate.Text == "")
                {
                    membrStartDate = commStart;
                    membrEndDate = commEnd;
                }
                else
                {
                    membrStartDate = Convert.ToDateTime(txtCommMembrStartDate.Text);
                    membrEndDate = Convert.ToDateTime(txtCommMembrEnDate.Text);
                }
                CommitteeMemberDTO committeeMemberDTO = new CommitteeMemberDTO
                {
                    CommitteId = new Guid(ddlCommMembrCommId.SelectedValue),
                    MemberId = new Guid(ddlCommMembrMembrId.SelectedValue),
                    MemberFrom = membrStartDate,
                    MemberTo = membrEndDate,
                    Position = ddlCommitteeMemberMemType.SelectedValue,
                    Description = txtCommitteeMemberDescription.Text,
                    //ModifiedBy = "Tharsan",
                    ModifiedTime = DateTime.Now
                };

                string result = memberAL.saveCommitteeMembers(committeeMemberDTO);
                if (result == "Saved")
                {
                    lblAddComMembrMsg.Text = "Committee Member Details added Successfully";
                    txtCommMembrStartDate.Text = "";
                    txtCommMembrEnDate.Text = "";
                    txtCommitteeMemberDescription.Text = "";

                }
                else if (result == "Updated")
                {
                    lblAddComMembrMsg.Text = "Committee Member Details updated Successfully";
                    txtCommMembrStartDate.Text = "";
                    txtCommMembrEnDate.Text = "";
                    txtCommitteeMemberDescription.Text = "";
                }
                else if (result == "AlreadyExists")
                {
                    lblAddComMembrMsg.Text = "The Committee Member with this role is already exists for this committee";
                    txtCommMembrStartDate.Text = "";
                    txtCommMembrEnDate.Text = "";
                    txtCommitteeMemberDescription.Text = "";
                }
                else if (result == "MemberPeriodInValid")
                {
                    lblAddComMembrMsg.Text = "The Committee Member's period does not fall under the selected Exceutive Committee";
                    txtCommMembrStartDate.Text = "";
                    txtCommMembrEnDate.Text = "";
                    txtCommitteeMemberDescription.Text = "";
                }
                else if (result == "onlyOneMemberOfthisRoleCanExists")
                {
                    lblAddComMembrMsg.Text = "Only one member can be added to this Committee on this designation";
                    txtCommMembrStartDate.Text = "";
                    txtCommMembrEnDate.Text = "";
                    txtCommitteeMemberDescription.Text = "";
                }

                else
                {
                    lblAddComMembrMsg.Text = "Your data cannot be added";
                    txtCommMembrStartDate.Text = "";
                    txtCommMembrEnDate.Text = "";
                    txtCommitteeMemberDescription.Text = "";
                }
            }
            catch (Exception ee)
            {
                lblAddComMembrMsg.Text = "There is an exception occured while inserting the Committee Member detail" + ee.ToString();
            }
        }

        

       

       


    }
}
